package cn.aitrox.ry.app.web.controller.anno;

import cn.aitrox.ry.app.web.config.AppNacosProperties;
import cn.aitrox.ry.app.web.service.SeriesService;
import cn.aitrox.ry.app.web.vo.series.in.SeriesTaskExportInVo;
import cn.aitrox.ry.common.bean.ExportFileCallback;
import cn.aitrox.ry.common.controller.BaseController;
import cn.aitrox.ry.util.ExcelUtil;
import cn.aitrox.ry.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class SeriesController extends BaseController {

    @Autowired
    private SeriesService seriesService;

    @Autowired
    private AppNacosProperties appNacosProperties;


    @PostMapping(value = "/series/task/export")
    public ResponseEntity seriesTaskExport(@Validated @RequestBody SeriesTaskExportInVo input) throws IOException {
        List<List<String>> exportData = seriesService.seriesTaskExport(input.getSeriesUids());
        File tmpParentFile = new File(appNacosProperties.getTmpFileParentPath() + UUIDUtil.getUUID());
        tmpParentFile.mkdirs();
        String fileName = "任务查询结果.xlsx";
        File file = new File(tmpParentFile, fileName);
        return exportFileUseByte(tmpParentFile, fileName, file, new ExportFileCallback() {
            @Override
            public File writeTmpFile(File exportFile) throws IOException {
                ExcelUtil.writeFile(exportData, file);
                return exportFile;
            }
        });
    }

}
